package gdut.jmxywz.action;

import gdut.jmxywz.been.Info;
import gdut.jmxywz.been.Roles;
import gdut.jmxywz.been.Teacher;
import gdut.jmxywz.been.TeacherRole;
import gdut.jmxywz.util.PageModel;
import gdut.jmxywz.util.ThreeDES;

import java.awt.Font;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.struts2.ServletActionContext;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;

import com.opensymphony.xwork2.ActionContext;

public class TeacherAction extends BaseAction {

	public static final String TEACHER_SESSION_KEY = "teacherSessionKey";
	
	private Teacher teacher;
	
	private String password;
	
	private String password2;
	
	private String password3;
	
	private String message="";
	
	private int teacherId;
	
	private List roleNotSelectedList;
	
	private List roleSelectedList;
	
	private TeacherRole teacherRole;
	
	private Roles roles;
	
	private ThreeDES threeDES;

	private int personalTag=0;
	
/*----------------照片上传----------------------*/
	private File pictureUpload;
	
	private String pictureUploadFileName;
	
	private String pictureUploadContentType;
	
	private String savePath="";

	
/*----------------分页----------------------*/
	private PageModel pageModel;
	
	private int currentPage=1;
	
	private int pageSize=5;

/*----------------检索----------------------*/
	private String searchSelect="";
	
	private String like="like";
	
	private String searchValue="";
	
	private String searchValue1;
	
	private String excelParameter="";

/*----------------Excel导入导出----------------------*/
	private File excelFile;
	
	private boolean checkbox;
	
/*---------------------结构图--------------------------*/
	private int pro1 = -1;
	
	private int pro2 = -1;
	
	private int total = -1;

	private int width = 400;
	
	private int height = 300;
	
	private String msg = " ";
	
/*----------------判断工号是否重复----------------------*/	
	private String teacherNum;
	
/***********************geter和setter方法*****************************/
	

	public Teacher getTeacher() {
		return teacher;
	}

	
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	
	
	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}
	
	
	public File getPictureUpload() {
		return pictureUpload;
	}

	public void setPictureUpload(File pictureUpload) {
		this.pictureUpload = pictureUpload;
	}

	public String getPictureUploadFileName() {
		return pictureUploadFileName;
	}

	public void setPictureUploadFileName(String pictureUploadFileName) {
		this.pictureUploadFileName = pictureUploadFileName;
	}

	public String getPictureUploadContentType() {
		return pictureUploadContentType;
	}

	public void setPictureUploadContentType(String pictureUploadContentType) {
		this.pictureUploadContentType = pictureUploadContentType;
	}

	public String getSavePath() {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	


	public PageModel getPageModel() {
		return pageModel;
	}

	public void setPageModel(PageModel pageModel) {
		this.pageModel = pageModel;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	
	public String getSearchSelect() {
		return searchSelect;
	}

	public void setSearchSelect(String searchSelect) {
		this.searchSelect = searchSelect;
	}

	public String getLike() {
		return like;
	}

	public void setLike(String like) {
		this.like = like;
	}

	public String getSearchValue() {
		return searchValue;
	}

	public void setSearchValue(String searchValue)throws UnsupportedEncodingException {
		this.searchValue = new String(searchValue.getBytes("ISO-8859-1"),"utf-8");
	}
	
	
	public int getTeacherId() {
		return teacherId;
	}


	public void setTeacherId(int teacherId) {
		this.teacherId = teacherId;
	}
	
	public File getExcelFile() {
		return excelFile;
	}


	public void setExcelFile(File excelFile) {
		this.excelFile = excelFile;
	}
	

	public boolean isCheckbox() {
		return checkbox;
	}


	public void setCheckbox(boolean checkbox) {
		this.checkbox = checkbox;
	}
	
	public String getExcelParameter() {
		return excelParameter;
	}


	public void setExcelParameter(String excelParameter) {
		this.excelParameter = excelParameter;
	}
	

	public List getRoleNotSelectedList() {
		return roleNotSelectedList;
	}


	public void setRoleNotSelectedList(List roleNotSelectedList) {
		this.roleNotSelectedList = roleNotSelectedList;
	}


	public List getRoleSelectedList() {
		return roleSelectedList;
	}


	public void setRoleSelectedList(List roleSelectedList) {
		this.roleSelectedList = roleSelectedList;
	}
	

	public TeacherRole getTeacherRole() {
		return teacherRole;
	}


	public void setTeacherRole(TeacherRole teacherRole) {
		this.teacherRole = teacherRole;
	}
	


	public Roles getRoles() {
		return roles;
	}


	public void setRoles(Roles roles) {
		this.roles = roles;
	}
	


	public ThreeDES getThreeDES() {
		return threeDES;
	}


	public void setThreeDES(ThreeDES threeDES) {
		this.threeDES = threeDES;
	}


	public int getPro1() {
		return pro1;
	}


	public void setPro1(int pro1) {
		this.pro1 = pro1;
	}


	public int getPro2() {
		return pro2;
	}


	public void setPro2(int pro2) {
		this.pro2 = pro2;
	}


	public int getTotal() {
		return total;
	}


	public void setTotal(int total) {
		this.total = total;
	}


	public int getWidth() {
		return width;
	}


	public void setWidth(int width) {
		this.width = width;
	}


	public int getHeight() {
		return height;
	}


	public void setHeight(int height) {
		this.height = height;
	}
	
	public String getMsg() {
		return msg;
	}


	public void setMsg(String msg) {
		this.msg = msg;
	}
	

	public String getSearchValue1() {
		return searchValue1;
	}


	public void setSearchValue1(String searchValue1) {
		this.searchValue1 = searchValue1;
	}
	

	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public String getPassword3() {
		return password3;
	}


	public void setPassword3(String password3) {
		this.password3 = password3;
	}


	public String getMessage() {
		return message;
	}


	public void setMessage(String message) {
		this.message = message;
	}
	

	public String getTeacherNum() {
		return teacherNum;
	}


	public void setTeacherNum(String teacherNum) {
		this.teacherNum = teacherNum;
	}
	

	public int getPersonalTag() {
		return personalTag;
	}


	public void setPersonalTag(int personalTag) {
		this.personalTag = personalTag;
	}


	/*################################################################################*/
	public String add() throws Exception {
		// TODO Auto-generated method stub
		/*由于jsp页面用到了struts标签，对应性别的value不接收中文等，所有只好进行转换*/
		if(teacher.getTeacherSex().equals("1")){
			teacher.setTeacherSex("男");
		}else{
			teacher.setTeacherSex("女");
		}
		/***
		 * 生成照片的路径和名称
		 */
		try{
		if(pictureUpload!=null){
			/*用当前时间组成字符串*/
			Date date=new Date();
			DateFormat df=new SimpleDateFormat("yyyy-MMddHHmmss");
			String dateString=df.format(date);
			Random ramdom=new Random();
			/*随机数生成字符串*/
			int ramdomInt=ramdom.nextInt(500);
			String randomString=String.valueOf(ramdomInt);
			for(int i=randomString.length();i<6;i++){
				randomString="0"+randomString;
			}
			/*组成照片路径和名称*/
			String pictureName = pictureUploadFileName;
			int tempIndexof = pictureName.lastIndexOf(".");
			pictureName = pictureName.substring(tempIndexof);
			pictureName = dateString + randomString + pictureName;
			/*读照片到指定目录中存放*/
			pictureName="\\teacherPicture\\"+pictureName;
			System.out.println("-------------重命名后的-"+pictureName+"----------------------------");
			FileInputStream fis=new FileInputStream(pictureUpload);
			FileOutputStream fos=new FileOutputStream(getSavePath()+pictureName);
			int len=0;
			byte[] buffer=new byte[1024];
			while((len=fis.read(buffer))>0){
				fos.write(buffer, 0, len);
			}
			teacher.setTeacherPic(pictureName);
			fis.close();
			fos.close();
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		/*密码加密后存数据库*/
		threeDES.getKey(threeDES.getTeacherKey());
		teacher.setTeacherPassword(threeDES.getEncString(teacher.getTeacherPassword()));
	
		boolean bl=this.getCommonService().saveObject(teacher);
		if(!bl){
			return "error";
		}
		
		return SUCCESS;
	}

	
	public String show() throws Exception {
		// TODO Auto-generated method stub
		String hql="from Teacher";
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"teacherShow.action","");
		/*转换日期显示格式*/
		if(pageModel.getList()!=null&&pageModel.getList().size()>0){
			for(int i=0;i<pageModel.getList().size();i++){
				teacher=(Teacher)pageModel.getList().get(i);
				if(teacher.getGraduatedTime()!=null){
				DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
				teacher.setGraduatedTime(df.parse(df.format(teacher.getGraduatedTime())));
				}
			}
			
		}
		return SUCCESS;
	}

	
	public String search() throws Exception {
		// TODO Auto-generated method stub
		String hql="from Teacher";
		String searchParameter="";
		
		searchValue1=searchValue;
		
		if (like.equals("like")) {
			hql = hql + " as bf  where bf." + searchSelect + " like '%"+ searchValue1 + "%'";
		} else {
			hql = hql + " as bf  where bf." + searchSelect + " ='"+ searchValue1 + "'";
		}
		
		String enSearchValue=java.net.URLEncoder.encode(searchValue1,"UTF-8");//URL编码转换
		/*用于Excel表导出exportExcel()方法中的查询带参跳页*/
		excelParameter="?searchSelect="+searchSelect+"&searchValue="+enSearchValue+"&like="+like;
		
		searchParameter="&searchSelect="+searchSelect+"&searchValue="+enSearchValue+"&like="+like;
		
		
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"teacherSearch.action",searchParameter);

		return SUCCESS;
	}

	
	public String detail() throws Exception {
		// TODO Auto-generated method stub
		teacher=(Teacher)this.getCommonService().findByIntId(teacherId, "gdut.jmxywz.been.Teacher");
		/*显示前进行照片路径特殊字符转换*/
		/*if(teacher.getTeacherPic()!=null){
			teacher.setTeacherPic(teacher.getTeacherPic().replace("\\","/").substring(1));
		}*/
		
		if(teacher==null)
			return "error";
		
		return SUCCESS;
		
	}


	
	public String preUpdate() throws Exception {
		// TODO Auto-generated method stub
		teacher=(Teacher)this.getCommonService().findByIntId(teacherId, "gdut.jmxywz.been.Teacher");
		if(teacher==null){
			return "error";
		}
		if(teacher.getTeacherSex().equals("男")){
			teacher.setTeacherSex("1");
		}else{
			teacher.setTeacherSex("2");
		}
		
		/*密码解密*/	
		threeDES.getKey(threeDES.getTeacherKey());
		teacher.setTeacherPassword(threeDES.getDesString(teacher.getTeacherPassword())); 
		
		/*if(teacher.getTeacherPic()!=null){
			teacher.setTeacherPic(teacher.getTeacherPic().replace("\\","/").substring(1));
		}*/
		return SUCCESS;
	}


	
	public String update() throws Exception {
		// TODO Auto-generated method stub
		/***
		 * 具体注释类似于增加方法add()中的注释
		 */
		if(teacher.getTeacherSex().equals("1")){
			teacher.setTeacherSex("男");
		}else{
			teacher.setTeacherSex("女");
		}

		if(pictureUpload==null){
			teacher.setTeacherPic("\\"+teacher.getTeacherPic().replace("/", "\\"));
		}
		
		try{
			if(pictureUpload!=null){
				Date date=new Date();
				DateFormat df=new SimpleDateFormat("yyyy-MMddHHmmss");
				String dateString=df.format(date);
				Random ramdom=new Random();
				int ramdomInt=ramdom.nextInt(500);
				String randomString=String.valueOf(ramdomInt);
				for(int i=randomString.length();i<6;i++){
					randomString="0"+randomString;
				}
			
				String pictureName = pictureUploadFileName;
				int tempIndexof = pictureName.lastIndexOf(".");
				pictureName = pictureName.substring(tempIndexof);
				pictureName = dateString + randomString + pictureName;
				
				pictureName="\\teacherPicture\\"+pictureName;
				System.out.println("-------------重命名后的-"+pictureName+"----------------------------");
				FileInputStream fis=new FileInputStream(pictureUpload);
				FileOutputStream fos=new FileOutputStream(getSavePath()+pictureName);
				int len=0;
				byte[] buffer=new byte[1024];
				while((len=fis.read(buffer))>0){
					fos.write(buffer, 0, len);
				}
				teacher.setTeacherPic(pictureName);
				fis.close();
				fos.close();
			}
			}catch(Exception e){
				e.printStackTrace();
			}
		
		
		
		threeDES.getKey(threeDES.getTeacherKey());
		teacher.setTeacherPassword(threeDES.getEncString(teacher.getTeacherPassword()));
	
		boolean bl=this.getCommonService().updateObject(teacher);
		if(!bl){
			return "error";
		}
		
		if(personalTag==1)
			return "personalSuccess";
		
		return SUCCESS;
	}


	public String delete() throws Exception {
		// TODO Auto-generated method stub
		/*删除教师记录之前，先删除教师角色对应关联*/
		String selectedRole="select tr from TeacherRole tr where tr.teacher.teacherId="+teacherId;
		List selectedRoleList=this.getCommonService().findListByHql(selectedRole, null);
		if(selectedRoleList!=null){
			for(int i=0;i<selectedRoleList.size();i++){
				TeacherRole tr=new TeacherRole();
				tr=(TeacherRole)selectedRoleList.get(i);
				
				boolean bl=this.getCommonService().deleteObject(tr);
				if(!bl){
					return "error";
				}
				
			}
		}
		
		teacher=(Teacher)this.getCommonService().findByIntId(teacherId, "gdut.jmxywz.been.Teacher");
		if(teacher==null){
			return "error";
		}
		/*删教师*/
		boolean bl=this.getCommonService().deleteObject(teacher);
		if(!bl){
			return "error";
		}
		return SUCCESS;
	}
	
	

	public String preGrant() throws Exception {
		// TODO Auto-generated method stub
		teacher=(Teacher)this.getCommonService().findByIntId(teacherId, "gdut.jmxywz.been.Teacher");
		/*授权之前，先查上未授权角色*/
		String hqlNotSelected="select r from Roles r where r not in (select tr.roles from TeacherRole tr where tr.teacher.teacherId="+teacherId+")";
		roleNotSelectedList=this.getCommonService().findListByHql(hqlNotSelected, null);
		/*已授权角色*/
		String hqlSelected="select tr.roles from TeacherRole tr where tr.teacher.teacherId="+teacherId;
		roleSelectedList=this.getCommonService().findListByHql(hqlSelected, null);
		
		if(roleNotSelectedList==null&&roleSelectedList==null){
			return "error";
		}
		return SUCCESS;
	}
	


	public String grant() throws Exception {
		// TODO Auto-generated method stub
		/*授权角色之前先删除所有已有角色*/
		String deleteList="select tr from TeacherRole tr where tr.teacher.teacherId="+teacher.getTeacherId();
		List olds=this.getCommonService().findListByHql(deleteList, null);
		for(int i=0;i<olds.size();i++){
			boolean d=this.getCommonService().deleteObject((TeacherRole)olds.get(i));
			if(!d){
				return"error";
			}
		}
		
		Teacher t=new Teacher();
		t=(Teacher)this.getCommonService().findByIntId(teacher.getTeacherId(), "gdut.jmxywz.been.Teacher");
		if(t==null){
			return "error";
		}
		/*重受角色*/
		for(int i=1;i<roleSelectedList.size();i++){
			TeacherRole tr=new TeacherRole();
			Roles r=new Roles();
			r=(Roles)this.getCommonService().findByIntId(Integer.parseInt(roleSelectedList.get(i).toString()), "gdut.jmxywz.been.Roles");
			if(r==null){
				return "error";
			}
			
			tr.setTeacher(t);
			tr.setRoles(r);
			tr.setTeacherRoleName(r.getRoleName());
			tr.setTeacherRoleDescripe(r.getRoleDescripe());
			
			boolean bl=this.getCommonService().saveObject(tr);
			if(!bl){
				return "error";
			}
		}
		return SUCCESS;
	}


	public String importExcel() throws Exception {
		// TODO Auto-generated method stub
		int count=0;
		Teacher teacher;
		Teacher temp=new Teacher();
	
		System.out.println("--------excel导入--------------------");
		if(excelFile==null)return "error";
		try{
			FileInputStream fis=new FileInputStream(excelFile);
	        jxl.Workbook jwb=Workbook.getWorkbook(fis);
	        int sheets=jwb.getNumberOfSheets();
	        for(int i=0;i<sheets;i++){
	        	Sheet rs=jwb.getSheet(i);
	        	int columnNum=rs.getColumns();
	        	System.out.println("---------------column数：-----------------"+columnNum);
	        	int rowNum=rs.getRows();
	        	if(rowNum>0&&columnNum!=11){
	        		System.out.println("---------------导入格式不对-----------------"+rowNum);
	        		return "error";
	        	}
	        	
	        	Cell cell;
	        	
	        	for(int row=1;row<rowNum;row++){
	        		
	        		cell=rs.getCell(0, row);
	        		System.out.println("--------------------------------"+cell.getContents());
	        		String hql="from Teacher t where t.teacherNum='"+cell.getContents()+"'";
	        		System.out.println("---------------程序经过-----------------"+hql);
	        		
	        		List l=this.getCommonService().findListByHql(hql, null);
	        		if(l.size()>=1){
	        			System.out.println("---------------程序经过1-----------------");
	        			teacher=(Teacher)l.get(0);
	        			cell=rs.getCell(1, row);
		        		teacher.setTeacherName(cell.getContents());
		        		cell=rs.getCell(2, row);
		        		teacher.setTeacherSex(cell.getContents());
		        		cell=rs.getCell(3, row);
		        		teacher.setTechnicalTitle(cell.getContents());
		        		cell=rs.getCell(4, row);
		        		teacher.setHeadship(cell.getContents());
		        		cell=rs.getCell(5, row);
		        		teacher.setTelephone(cell.getContents());
		        		cell=rs.getCell(6, row);
		        		teacher.setEmail(cell.getContents());
		        		cell=rs.getCell(7, row);
		        		teacher.setTeacherEducation(cell.getContents());
		        		cell=rs.getCell(8,row);
		        		teacher.setTeacherDegree(cell.getContents());
		        		cell=rs.getCell(9, row);
		        		teacher.setGraduatedSchool(cell.getContents());
		        		
		        		cell=rs.getCell(10, row);
		        		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		        		if(cell.getContents().toString()==null){
			        		System.out.println("=================*******空啊******=================");
		        		}
		        		if(cell.getContents().toString().trim()==""){
			        		System.out.println("=================*******空啊5******=================");
		        		}
		        		System.out.println("=================$$$$================="+cell.getContents());
		        		if(cell.getContents()!=null&&cell.getContents().trim()!=""){
		        			teacher.setGraduatedTime(sdf.parse(cell.getContents().toString()));
		        		}
		        		
		        		
		        		boolean bl=this.getCommonService().updateObject(teacher);
		        		if(!bl){
		        			System.out.println("=================修改数据库出错=================");
		        		}
	        		}else{
	        			
	        			
	        			Teacher teacher2=new Teacher();
	        			teacher2.setTeacherNum(cell.getContents());
	        			
	        			threeDES.getKey(threeDES.getTeacherKey());
	        			teacher2.setTeacherPassword(threeDES.getEncString(cell.getContents())); 
	        		
	        		cell=rs.getCell(1, row);
	        		teacher2.setTeacherName(cell.getContents());
	        		cell=rs.getCell(2, row);
	        		teacher2.setTeacherSex(cell.getContents());
	        		cell=rs.getCell(3, row);
	        		teacher2.setTechnicalTitle(cell.getContents());
	        		cell=rs.getCell(4, row);
	        		teacher2.setHeadship(cell.getContents());
	        		cell=rs.getCell(5, row);
	        		teacher2.setTelephone(cell.getContents());
	        		cell=rs.getCell(6, row);
	        		teacher2.setEmail(cell.getContents());
	        		cell=rs.getCell(7, row);
	        		teacher2.setTeacherEducation(cell.getContents());
	        		cell=rs.getCell(8,row);
	        		teacher2.setTeacherDegree(cell.getContents());
	        		cell=rs.getCell(9, row);
	        		teacher2.setGraduatedSchool(cell.getContents());
	        		
	        		cell=rs.getCell(10, row);
	        		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	        		teacher2.setGraduatedTime(sdf.parse(cell.getContents()));
	        		System.out.println("---------------程序经过2-----------------");
	        		System.out.println("+++++++++++++++++++++++++++++"+teacher2.getTeacherNum()+teacher2.getTeacherName()+teacher2.getTeacherPassword());
	        		System.out.println("+++++++++++++++++++++++++++++"+teacher2.getGraduatedTime()+teacher2.getGraduatedSchool()+teacher2.getTelephone());
	        		boolean	bl=this.getCommonService().saveObject(teacher2);
	        		
	        		if(!bl){
	        			System.out.println("=================存入数据库出错=================");
	        			}
	        		}
	        		
	        		
	        		count++;
	        	}
	        	
	        	
	        }
	        System.out.println("=================一共导入数据数：================="+count);
	       
	        jwb.close();    	
	        fis.close();
			}catch(Exception e){
				e.printStackTrace();
				
			}
		return SUCCESS;
	}


	
	public void exportExcel() throws Exception {
		// TODO Auto-generated method stub
		/*导出EXCEL之前可以先查询记录，找到后再导出*/
		try{
			String[] str=new String[]{"工号","姓名","性别","职称","职务","联系方式","邮箱","学历","学位","毕业院校","毕业时间"};
			List list;
			
			if(!searchSelect.equals("")){
				String hql="from Teacher";
				String searchValue1=searchValue;
				
				if (like.equals("like")) {
					hql = hql + " as bf  where bf." + searchSelect + " like '%"+ searchValue1 + "%'";
				} else {
					hql = hql + " as bf  where bf." + searchSelect + " ='"+ searchValue1 + "'";
				}
				
				list=this.getCommonService().findListByHql(hql, null);
			}else{
				String hql="from Teacher";
				list=this.getCommonService().findListByHql(hql, null);
			
			}
			if(list==null)return;

			HttpServletResponse response=ServletActionContext.getResponse();
			response.reset();
			response.setContentType("application/vnd.ms-excel");
			response.setContentType("APPLICATION/OCTET-STREAM ");
			response.setHeader("Content-Disposition","attachment; filename="+java.net.URLEncoder.encode("教师.xls","UTF-8"));
			WritableWorkbook wwb=Workbook.createWorkbook(response.getOutputStream());
			WritableSheet sheet=wwb.createSheet("sheet1", 0);
			sheet.setColumnView(0, 20);
			sheet.setColumnView(1, 20);
			sheet.setColumnView(3, 20);
			sheet.setColumnView(4, 20);
			sheet.setColumnView(5, 20);
			sheet.setColumnView(6, 20);
			sheet.setColumnView(7, 20);
			sheet.setColumnView(8, 20);
			sheet.setColumnView(9, 20);
			sheet.setColumnView(10, 20);
			
			Label label=null;
			for(int i=0;i<str.length;i++){
				label=new Label(i,0,str[i]);
				sheet.addCell(label);
			}
			for(int i=0;i<list.size();i++){
				teacher=(Teacher)list.get(i);
				
				label=new Label(0,i+1,teacher.getTeacherNum());
				sheet.addCell(label);
				label=new Label(1,i+1,teacher.getTeacherName());
				sheet.addCell(label);
				label=new Label(2,i+1,teacher.getTeacherSex());
				sheet.addCell(label);
				label=new Label(3,i+1,teacher.getTechnicalTitle());
				sheet.addCell(label);
				label=new Label(4,i+1,teacher.getHeadship());
				sheet.addCell(label);
				label=new Label(5,i+1,teacher.getTelephone());
				sheet.addCell(label);
				label=new Label(6,i+1,teacher.getEmail());
				sheet.addCell(label);
				label=new Label(7,i+1,teacher.getTeacherEducation());
				sheet.addCell(label);
				label=new Label(8,i+1,teacher.getTeacherDegree());
				sheet.addCell(label);
				label=new Label(9,i+1,teacher.getGraduatedSchool());
				sheet.addCell(label);
		
				if(teacher.getGraduatedTime()!=null){
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					label=new Label(10,i+1,sdf.format(teacher.getGraduatedTime()));
					sheet.addCell(label);
				}else{
					label=new Label(10,i+1,"");
					sheet.addCell(label);
				}
				
			}
			wwb.write();
			wwb.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

/**
 * 教师结构图生成
 * 参考旧系统代码
 * @return
 * @throws Exception
 */
	public String pro() throws Exception {
		// TODO Auto-generated method stub
		msg="";
		if (total<pro1 + pro2){
			msg = "输入数字有误，请重新输入";
		}else{
			DefaultPieDataset dpd = new DefaultPieDataset();
			
			dpd.setValue("教授" + ((float) (pro1 * 10000 / total)) / 100 + "%", pro1);
			dpd.setValue("副教授" + ((float) (pro2 * 10000 / total)) / 100 + "%",pro2);
			dpd.setValue("其它" + ((float) ((total - pro1 - pro2) * 10000 / total))/ 100 + "%", total - pro1 - pro2);

			Font font = new Font("宋体", Font.PLAIN, 14);
			/*创建饼图*/
			JFreeChart jc = ChartFactory.createPieChart3D("教师职称结构", dpd, true,true, Locale.CHINESE);
			/*设置图的题目的格式*/
			TextTitle txtTitle = null;
			txtTitle = jc.getTitle();
			txtTitle.setFont(new Font("宋体", Font.BOLD, 16));
			/*图的正文和底部格式*/
			PiePlot pieplot = (PiePlot) jc.getPlot();
			pieplot.setLabelFont(font);
			jc.getLegend().setItemFont(font);

			try {
				/*重画饼图之前，先删除原来的饼图*/
				File file=new File(ServletActionContext.getServletContext().getRealPath("/")+"\\admin\\teacher\\image\\teacherPro.jpg");
				file.delete();
				/*生成饼图图片，并存到指定目录*/
				ChartUtilities.saveChartAsJPEG(new File(ServletActionContext.getRequest().getSession().getServletContext().getRealPath(
								"\\admin\\teacher\\image\\teacherPro.jpg")), jc, width,height);
				msg = "成功修改职称结构图";
			} catch (IOException e){
				msg = "修改不成功，请重新尝试";
			}
		}
		
		return SUCCESS;
	}
	
	public String deg() throws Exception {
		// TODO Auto-generated method stub
		/***
		 * 注释同上方法注释
		 */
		msg="";
		
		if (total<pro1 + pro2){
			msg = "输入数字有误，请重新输入";
		}else{
			DefaultPieDataset dpd = new DefaultPieDataset();
			
			dpd.setValue("博士" + ((float) (pro1 * 10000 / total)) / 100 + "%", pro1);
			dpd.setValue("硕士" + ((float) (pro2 * 10000 / total)) / 100 + "%",pro2);
			dpd.setValue("其它" + ((float) ((total - pro1 - pro2) * 10000 / total))/ 100 + "%", total - pro1 - pro2);

			Font font = new Font("宋体", Font.PLAIN, 14);

			JFreeChart jc = ChartFactory.createPieChart3D("教师学历结构", dpd, true,true, Locale.CHINESE);

			TextTitle txtTitle = null;
			txtTitle = jc.getTitle();
			txtTitle.setFont(new Font("宋体", Font.BOLD, 16));

			PiePlot pieplot = (PiePlot) jc.getPlot();
			pieplot.setLabelFont(font);
			jc.getLegend().setItemFont(font);

			try {
				File file=new File(ServletActionContext.getServletContext().getRealPath("/")+"\\admin\\teacher\\image\\teacherDeg.jpg");
				file.delete();
				ChartUtilities.saveChartAsJPEG(new File(ServletActionContext.getRequest().getSession().getServletContext().getRealPath(
								"\\admin\\teacher\\image\\teacherDeg.jpg")), jc, width,height);
				msg = "成功修改学历结构图";
			} catch (IOException e){
				msg = "修改不成功，请重新尝试";
			}
		}
		
		return SUCCESS;
	}
	
	public String age() throws Exception {
		// TODO Auto-generated method stub
		/**
		 * 注释同上
		 */
		msg="";
		if (total<pro1 + pro2){
			msg = "输入数字有误，请重新输入";
		}else{
			DefaultPieDataset dpd = new DefaultPieDataset();
			
			dpd.setValue("35岁以下" + ((float) (pro1 * 10000 / total)) / 100 + "%", pro1);
			dpd.setValue("36岁到45岁" + ((float) (pro2 * 10000 / total)) / 100 + "%",pro2);
			dpd.setValue("46岁以上" + ((float) ((total - pro1 - pro2) * 10000 / total))/ 100 + "%", total - pro1 - pro2);

			Font font = new Font("宋体", Font.PLAIN, 14);

			JFreeChart jc = ChartFactory.createPieChart3D("教师年龄结构", dpd, true,true, Locale.CHINESE);

			TextTitle txtTitle = null;
			txtTitle = jc.getTitle();
			txtTitle.setFont(new Font("宋体", Font.BOLD, 16));

			PiePlot pieplot = (PiePlot) jc.getPlot();
			pieplot.setLabelFont(font);
			jc.getLegend().setItemFont(font);

			try {
				File file=new File(ServletActionContext.getServletContext().getRealPath("/")+"\\admin\\teacher\\image\\teacherAge.jpg");
				file.delete();
				ChartUtilities.saveChartAsJPEG(new File(ServletActionContext.getRequest().getSession().getServletContext().getRealPath(
								"\\admin\\teacher\\image\\teacherAge.jpg")), jc, width,height);
				msg = "成功修改年龄结构图";
			} catch (IOException e){
				msg = "修改不成功，请重新尝试";
			}
		}
		
		return SUCCESS;
	}
	

	
	public String preModifyPwd() throws Exception {
		// TODO Auto-generated method stub
		
		return SUCCESS;
	}


	public String modifyPwd() throws Exception {
		// TODO Auto-generated method stub
		message="";
		
		teacher=(Teacher)this.getCommonService().findByIntId(teacherId, "gdut.jmxywz.been.Teacher");
		if(teacher==null){
			return "error";
		}
		/*密码解密*/
		threeDES.getKey(threeDES.getTeacherKey());
		teacher.setTeacherPassword(threeDES.getDesString(teacher.getTeacherPassword())); 
		
		if(password!=null&&password.equals(teacher.getTeacherPassword())){
			if(password2==null||password3==null||password2.trim().equals("")||password3.trim().equals("")){
				message="新密码不能为空，请输入新密码...";
				return "input";
			}
			if(password2.equals(password3)){	
				/*密码加密*/
				threeDES.getKey(threeDES.getTeacherKey());
				teacher.setTeacherPassword(threeDES.getEncString(password2));
				boolean bl=this.getCommonService().updateObject(teacher);
				if(!bl) return "error";
				message="密码修改成功，请记住你的密码！";
				return SUCCESS;
			}else{
				message="两次输入的新密码不一致，请重新输入...";
				return "input";
			}
		}else{
			message="旧密码为空或输入错误，不能修改密码，请重新输入...";
			return "input";
		}
		
	}
/**
 * 异步实现校验新增工号是否存在
 * @throws Exception
 */
	public void validateNum() throws Exception {
		// TODO Auto-generated method stub
		
		 HttpServletResponse res = ServletActionContext.getResponse();
		 res.setCharacterEncoding("utf-8");
		 res.setContentType("text/xml");
		 PrintWriter out = null;
		 if(teacherNum!=null){
			 String hql="select t from Teacher t where t.teacherNum='"+teacherNum+"'";
			 List teacherCheckList=this.getCommonService().findListByHql(hql, null);
			 /*如果新增工号在数据库中存在，则返回字符串true，作为js文件处理标记*/
			 if(teacherCheckList.size()>0){
				 try {   
			            out = res.getWriter();   
			            out.print("<root>");   
			            out.print("<message>");   
			            out.print("true");   
			            out.print("</message>");   
			            out.print("</root>");   
			        } catch (Exception e) {   
			            e.printStackTrace();   
			        } finally {   
			            out.flush();   
			            out.close();   
			        }   
			 }
		 }
		 
	}

/**
 * 下面是个人信息显示和修改
 * @return
 * @throws Exception
 */
	/*个人教师详情，该action主要是要判断个人session和带teacherId到detail方法*/
	public String personalDetail() throws Exception {
		// TODO Auto-generated method stub
		teacher=(Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		if(teacher==null)
			return "input";
		teacherId=teacher.getTeacherId();
		return SUCCESS;
	}

	/*个人教师修改，目的同上*/
	public String personalPreUpdate() throws Exception {
		// TODO Auto-generated method stub
		teacher=(Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		if(teacher==null)
			return "input";
		teacherId=teacher.getTeacherId();
		personalTag=1;
		return SUCCESS;
	}

	
}
